package com.example.rbacmanage.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import java.util.ArrayList;
import java.util.List;

public class AlipayUtil {
    /**
     * 生成小程序二维码
     * @param appId 应用id
     * @param privateKey 应用私钥
     * @param publicKey 支付宝公钥
     * @param content 参数
     */
    public static String createQrCode(String appId,String privateKey,String publicKey,String content){
        AlipayClient alipayClient = configAlipay(appId,privateKey,publicKey);

        // 构造请求参数以调用接口
        AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
        AlipayOpenAppQrcodeCreateModel model = new AlipayOpenAppQrcodeCreateModel();

        // 设置跳转小程序的页面路径
        model.setUrlParam("page/component/component-pages/view/view");

        // 设置小程序的启动参数
        model.setQueryParam(content);

        // 设置码描述
        model.setDescribe("二维码描述");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayOpenAppQrcodeCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response.getBody();
    }

    /**
     * 配置支付宝支付
     * @param appId 应用id
     * @param privateKey 应用私钥
     * @param publicKey 支付宝公钥
     * @return
     */
    public static AlipayClient configAlipay(String appId,String privateKey,String publicKey){
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("utf-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setAlipayPublicKey(publicKey);
        try {
            return new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建交易订单
     * @return 获得支付宝交易号 tradeNO
     */
    public static String createPayOrder(String appId,String privateKey,String publicKey){
        AlipayClient alipayClient = configAlipay(appId,privateKey,publicKey);
        // 构造请求参数以调用接口
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        AlipayTradeCreateModel model = new AlipayTradeCreateModel();
        // 设置商户订单号
        model.setOutTradeNo("20150320010101001");
        // 设置产品码
        model.setProductCode("JSAPI_PAY");
        // 设置小程序支付中
        model.setOpAppId("2014072300007148");
        // 设置订单总金额
        model.setTotalAmount("88.88");
        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setTradeComponentOrderId("2023060801502300000008810000005657");
        model.setExtendParams(extendParams);
        // 设置可打折金额
        model.setDiscountableAmount("80.00");
        // 设置订单标题
        model.setSubject("Iphone6 16G");
        // 设置订单附加信息
        model.setBody("Iphone6 16G");
        // uid参数未来计划废弃，存量商户可继续使用，新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
        // model.setBuyerId("2088102146225135");
        // 设置买家支付宝用户唯一标识
        model.setBuyerOpenId("074a1CcTG1LelxKe4xQC0zgNdId0nxi95b5lsNpazWYoCo5");
        // 设置商户门店编号
        model.setStoreId("NJ_001");
        request.setBizModel(model);
        request.setNotifyUrl("http://www.baidu.com");
        request.setReturnUrl("http://www.baidu.com");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response.getBody();
    }
    /**
     * 统一收单交易查询
     */
    public static AlipayTradeQueryResponse queryOrder(String appId,String privateKey,String publicKey,String outTradeNo){
        AlipayClient alipayClient = configAlipay(appId,privateKey,publicKey);
        // 构造请求参数以调用接口
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo(outTradeNo);

        // 设置支付宝交易号
        model.setTradeNo("2014112611001004680 073956707");

        // 设置银行间联模式下有用
//        model.setOrgPid("2088101117952222");

        // 设置查询选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("trade_settle_info");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response;
    }

    /**
     * 统一收单交易关闭接口
     * @param outTradeNo 商户订单号
     */
    public static void closeOrder(String appId,String privateKey,String publicKey,String outTradeNo){
        AlipayClient alipayClient = configAlipay(appId,privateKey,publicKey);
        // 构造请求参数以调用接口
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        AlipayTradeCloseModel model = new AlipayTradeCloseModel();

        // 设置该交易在支付宝系统中的交易流水号
//        model.setTradeNo("2013112611001004680073956707");

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo(outTradeNo);

        // 设置商家操作员编号 id
        model.setOperatorId("YX01");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeCloseResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}
